function refaseUser(){
    tableIn.reload("User");
}

var tableIn = null;

var index;
var tableData;
var table;
var form;

layui.use(['table','form'], function(){
    table = layui.table;
    form = layui.form;
    //第一个实例
    tableIn = table.render({
        elem: '#demo'
        ,url: '/Primary/getAllz' //数据接口
        ,id: 'User'
        ,page: true //开启分页
        ,page:{
            layout:['count','prev','page','total','next','limit'],//展示的按钮
            groups:10,//展示的联想的页码数
            limit:10,//每页的条数
            limits:[10,20],//可选的每页条数
        },cols: [[ //表头
            {type:'checkbox', width: 80}
            ,{field: 'userId', title: 'ID', width:100,align:'center'}
            ,{field: 'userNumber', title: '用户编号', width:100,align:'center'}
            ,{field: 'avatarUrl', title: '用户头像',width:100,hide:'true',templet:function (data){
                    return '<img src="'+data.avatarUrl+'" style="height: 55px;width: 50px;">';
                }}
            ,{field: 'userAccount', title: '用户账号',width: 100,align:'center'}
            ,{field: 'userNickname', title: '用户昵称',width: 100,align:'center'}
            ,{field: 'stuName', title: '学生姓名',width: 100,align:'center'}
            ,{field: 'userSex', title: '性别',width: 100,align:'center'
                ,templet:function (data){
                    if (data.userSex == 0){
                        return '男';
                    }else if (data.userSex == 1){
                        return '女';
                    }
                }}
            ,{field: 'styleDate', title: '学习时长',width: 100,align:'center',hide:'true'}
            ,{field: 'userAmount', title: '用户消费总金额',width: 100,align:'center',hide:'true'}
            ,{field: 'createDate', title: '用户创建时长',align:'center'}
            ,{field: 'statuss', title: '封禁状态',width: 100,align:'center',templet: function (d){
                    let menuId=d.userId;
                    if(d.statuss == 0){
                        return " <input type='checkbox' menuId='"+menuId+"' lay-filter='statuss' lay-skin='switch' lay-text='正常|封禁' checked>"
                    }else if (d.statuss == 1){
                        return " <input type='checkbox' menuId='"+menuId+"' lay-filter='statuss' lay-skin='switch' lay-text='正常|封禁'>"
                    }
                }}
            ,{field: 'banned', title: '封禁原因',width: 100,hide:'true',align:'center'}
            ,{field: 'isd', title: '用户分辨',align:'center',templet:function (d){
                if (d.isd == 0){
                    return "进思用户";
                }else if (d.isd == 1){
                    return "非进思用户";
                }
            }}
            ,{field: 'cz', title: '操作', toolbar: '#barDemo',align:'center'}
        ]], parseData:function (res){
            return{
                "code":res.state,//解析接口状态
                "msg":res.message,//解析提交文本
                "count":res.total,//解析数据长度
                "data":res.data//解析数据列表
            };
        },done:function(){
            tableData = table.cache.User;
        }
        ,toolbar: '#toolDemo'
    });

    form.on('switch(statuss)', function(obj){
        //根据业务判断是开启还是关闭
        var statuss = obj.elem.checked? 0 : 1 ;
        //方法一取数据（根据相对位置取）
        // var id = obj.othis.parents('tr').find("td :first").text();
        //方法二取数据 （根据索引table.cache里面的行数据）
        var index  = obj.othis.parents('tr').attr("data-index");
        var userId = tableData[index].userId;

        if (statuss == 1){
            layer.open({
                type:2,
                title:'封禁原因',
                content:'Banned.html',
                area:['35%','35%'],
                success: function (layero, index) {
                    // 获取iframe层dom对象
                    let childFrame = layer.getChildFrame('body', index);
                    //操作iframe层dom对象
                    childFrame.find('input[name="userId"]').val(userId);
                }
            })
        }
        $.get("/advanced/upStatuss",{"userId":userId,"statuss":statuss},function (res) {
            if(res.code != '0'){
                refaseUser();
                if(top.window['title']){
                    top.window['title'].frames.location.reload();
                }
            }
        });
    });


    // 监听工具条
    table.on('tool(test)', function (obj) {//注：tool是工具条时间名，test是table
        // console.log(obj.data.userId);
        // console.log(obj.data.cdate);
        // console.log(obj.data);
        var e = obj.event;

        if (e === 'view') {
            // layer.msg("查看"+data.id+data.username);
            layer.open({
                type: 2,
                title: '查看',
                content: 'View.html',
                area: ['50%', '90%'],
                id: 'User',
                success: function (layero, index) {//弹出层成功弹出后的回调函数

                    // 父页面向子页面传参 -- iframe 层传数据  获取iframe页面
                    var body = layer.getChildFrame('body', index);
                    body.find('#avatarUrl').attr('src',obj.data.avatarUrl);
                    body.find('#userAccount').val(obj.data.userAccount);
                    body.find('#userNickname').val(obj.data.userNickname);
                    body.find('#stu_id').val(obj.data.stu_id);
                    body.find('#styleDate').val(obj.data.styleDate);
                    body.find('#freestyle').val(obj.data.freestyle);
                    body.find('#idCard').val(obj.data.idCard);
                    body.find('#userSex').val(obj.data.userSex);
                    body.find('#userEmail').val(obj.data.userEmail);
                    body.find('#userAmount').val(obj.data.userAmount);
                    body.find('#createDate').val(obj.data.createDate);
                    body.find('#watchNum').val(obj.data.watchNum);
                    body.find('#collectionNum').val(obj.data.collectionNum);
                    body.find('#thumbNum').val(obj.data.thumbNum);

                }
            })
        } else if (e === 'edit') {

            localStorage.setItem("url",obj.data.avatarUrl);
            localStorage.setItem("userSex",obj.data.userSex);


            layer.open({
                type: 2,
                title: '修改',
                content: 'Userdeit.html',
                area: ['50%', '90%'],
                success: function (layero, index) {//弹出层成功弹出后的回调函数
                    // 获取iframe层dom对象
                    let childFrame = layer.getChildFrame('body', index);
                    //操作iframe层dom对象
                    // childFrame.find('#avatarUrl').attr('src',obj.data.avatarUrl);
                    childFrame.find('input[name="avatarUrl"]').val(obj.data.avatarUrl);
                    childFrame.find('input[name="userAccount"]').val(obj.data.userAccount);
                    childFrame.find('input[name="userNickname"]').val(obj.data.userNickname);
                    childFrame.find('input[name="freestyle"]').val(obj.data.freestyle);
                    childFrame.find('input[name="idCard"]').val(obj.data.idCard);
                    childFrame.find('input[name="phone"]').val(obj.data.phone);
                    childFrame.find('input[name="userEmail"]').val(obj.data.userEmail);
                    childFrame.find('input[name="userSex"]').val(obj.data.userSex);
                    childFrame.find('input[name="userAmount"]').val(obj.data.userAmount);
                    childFrame.find('input[name="styleDate"]').val(obj.data.styleDate);
                    childFrame.find('input[name="createDate"]').val(obj.data.createDate);
                    childFrame.find('input[name="userId"]').val(obj.data.userId);

                }
            })
        }else if (e === 'closure'){
            layer.open({
                type: 2,
                title: '封号',
                content: 'Stop.html',
                area: ['50%', '45%'],
                success: function (layero, index) {
                    // 获取iframe层dom对象
                    let childFrame = layer.getChildFrame('body', index);
                    //操作iframe层dom对象
                    childFrame.find('input[name="userAccount"]').val(obj.data.userAccount);
                    childFrame.find('input[name="userId"]').val(obj.data.userId);
                }
            });
        }

    });

    table.on('toolbar(test)', function (obj) {

        var e = obj.event;

        if(e === 'addjs'){
            //打开弹出层
            layer.open({
                type:2,
                title:'添加为进思用户',
                content:'Addjs.html',
                area:['60%','60%']
            })
        }
    });

    form.on('submit(go)',function (d){
        d = d.field;
        table.reload('User',{
            where:{
                userAccount:d.userAccount,
                userNickname: d.userNickname,
                createDate: d.createDate
            },page:{
                page: 1,
                limit:10
            }
        })
    });

    $('#heui').click(function (){
        layer.open({
            type:2,
            title:'高级检索',
            content:'Senior.html',
            area:['60%','60%']
        })
    })
});
function shai(userAccount,idCard,phone,userEmail){
    layui.use('table', function () {
        var table=layui.table;

        table.reload('User',{
            where:{
                userAccount:userAccount,
                idCard: idCard,
                phone: phone,
                userEmail: userEmail
            },page:{
                page: 1,
                limit:10
            }
        })
    })
    }
